package service

import (
	"dssh/common"
	"dssh/models"
	"gitee.com/dingyangzhuang/dingutils/dlog/logger"
	"gitee.com/dingyangzhuang/dingutils/encryption"
	"gitee.com/dingyangzhuang/dingutils/wechat"
	"strings"
)

type ICustomService interface {
	Execute(request models.ModelCustomRequest) (string, error)
}

type CustomService struct {
}

func (c CustomService) Execute(request models.ModelCustomRequest) (string, error) {
	//TODO implement me
	if request.Instruct == "execute" {
		var argumentMap = make(map[string]interface{}, 0)
		argumentMap["description"] = "execute"
		argumentMap[request.Ip] = strings.Split(request.Args, ";;")
		//解密
		decrypt, err := encryption.PasswdDecrypt(request.Password, []byte(common.EncryptionKey))
		if err != nil {
			logger.Error("密码解密错误", "password", request.Password)
			return "", err
		}
		logger.Info("密码解密结果：", "password", decrypt)
		msg, err := common.ExecuteDefineCmd(request.Ip, "22", request.User, decrypt, argumentMap, common.UserDefineCmdSsh)
		if err != nil {
			wechat.SendWechatMsgToOne(err.Error(), "dingyz", common.NACOSCONFIGS.Wechat.Agentid,
				common.NACOSCONFIGS.Wechat.Corpid, common.NACOSCONFIGS.Wechat.Corpsecret)
			return "", err
		}
		return msg, nil
	}

	//获取操作系统信息，或者不同系统执行不同指令
	if request.Instruct == "getserverinfo" {
		var cmdMap = make(map[string]string, 0)
		var user_define_cmds = make(map[string]interface{}, 0)
		split := strings.Split(request.Args, ";;")
		field := split[1]
		cmdMap["ubuntu"] = split[2]
		cmdMap["euler"] = split[3]
		cmdMap["centos"] = split[4]

		user_define_cmds[request.Ip] = strings.Split(request.Args, ";")
		user_define_cmds["field"] = field
		user_define_cmds["cmdMap"] = cmdMap
	}
	return "", nil
}
